package com.agriculture.controller;

import com.agriculture.common.R;
import com.agriculture.entity.Star;
import com.agriculture.entity.User;
import com.agriculture.service.StarService;
import com.agriculture.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品收藏
 */

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("star")
public class StarController {

    @Autowired
    private StarService starService;

    /**
     * 新增收藏
     * @param star
     * @return
     */
    @PostMapping
    public R<String> addStar(@RequestBody Star star){
        starService.save(star);
        return R.success("收藏成功！");
    }

    /**
     * 查询某个用户的全部收藏
     * @param userId
     * @return
     */
    @GetMapping("/list")
    private R<List<Star>> getAll(Long userId){
        LambdaQueryWrapper<Star> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Star::getUserId, userId);
        wrapper.orderByDesc(Star::getUpdateTime);
        return R.success(starService.list(wrapper));
    }

    /**
     * 根据 id 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/delById")
    public R<String> delById(@RequestParam Long id){
        starService.removeById(id);
        return R.success("删除成功！");
    }

    /**
     * 根据 token 删除 star
     * @param token
     * @return
     */
    @DeleteMapping("/delByToken")
    public R<String> delByToken (@RequestParam String token){
        User user = JwtUtils.parseToken(token);
        Long userId = user.getId();
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Star::getUserId, userId);
        starService.remove(queryWrapper);
        return R.success("删除成功！");
    }

    /**
     * 根据 good_id 删除 star
     * @param goodId
     * @return
     */
    @DeleteMapping("/delByGoodId")
    public R<String> delByGoodId(@RequestParam Long goodId){
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Star::getGoodId, goodId);
        starService.remove(queryWrapper);
        return R.success("删除成功！");
    }

    /**
     * 根据 good_id 删除 star
     * @param goodId
     * @return
     */
    @DeleteMapping("/delByTokenGoodId")
    public R<String> delByGoodId(@RequestParam Long goodId,@RequestParam String token){
        User user = JwtUtils.parseToken(token);
        Long userId = user.getId();
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Star::getUserId, userId);
        queryWrapper.eq(Star::getGoodId, goodId);
        starService.remove(queryWrapper);
        return R.success("删除成功！");
    }

    /**
     * 根据 token 和 good_id 查询 star
     * @param token
     * @param goodId
     * @return
     */
    @GetMapping("getByTokAGoodId")
    public R getByTokAGoodId(@RequestParam String token, @RequestParam Long goodId){
        User user = JwtUtils.parseToken(token);
        Long userId = user.getId();
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Star::getUserId, userId);
        queryWrapper.eq(Star::getGoodId, goodId);
        return R.success(starService.list(queryWrapper));
    }
}
















